<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>登录</title>
    <script src="./jQuery.js"></script>
  </head>
  <body>
    <div>
      <label> 用户名 <input type="text" id="textUsername" /> </label>
      <label> 密码 <input type="password" id="textPassword" /> </label>
      <button id="btnLogin">登录</button>
    </div>
    <script>
      // 发送 post 请求
      function post(url, data = {}) {
        return $.ajax({
          type: "post",
          url,
          data: JSON.stringify(data),
          contentType: "application/json",
        });
      }

      $("#btnLogin").click(() => {
        const username = $("#textUsername").val();
        const password = $("#textPassword").val();
        const url = "/api/user/login";
        const data = {
          username,
          password,
        };
        post(url, data).then((res) => {
          if (res.errno === 0) {
            location.href = "./admin.html";
          } else {
            alert(res.message);
          }
        });
      });
    </script>
  </body>
</html>
